<template>
  <div class="container">
    <div class="close"><span class="iconfont iconicon-test"></span></div>
    <div class="logo"><span class="iconfont iconnew"></span></div>
    <div class="inputs">
      <myinput
        placeholder="请输入手机号码"
        :rules="/^1[3456789]\d{9}$/"
        msg="请输入11位手机号码"
        v-model="user.username"
      ></myinput>
      <myinput
        placeholder="请输入昵称"
        v-model="user.nickname"
        :rules="/./"
        msg="昵称不能为空"
      ></myinput>
      <myinput
        placeholder="请输入密码"
        :rules="/^.{3,16}$/"
        msg="请输入3~16位的密码"
        v-model="user.password"
      ></myinput>
    </div>
    <p class="tips">
      有账号？
      <a href="#/login" class="">去登录</a>
    </p>
    <mybutton type="success" @click="register">注册</mybutton>
  </div>
</template>

<script>
import mybutton from "@/components/mybutton.vue";
import myinput from "@/components/myinput.vue";
import { userRegister } from "@/apis/user.js";
export default {
  components: {
    mybutton,
    myinput,
  },
  data() {
    return {
      // 定义一组数据，发送请求
      user: {
        username: "",
        password: "",
        nickname: "",
      },
    };
  },
  methods: {
    register() {
      // 如果条件成立，就发送请求
      if (
        /^1[3456789]\d{9}$/.test(this.user.username) &&
        /^.{3,16}$/.test(this.user.password) &&
        this.user.nickname.length > 0
      ) {
        console.log("1");
        //   console.log(this.user);
        userRegister(this.user)
          .then((res) => {
            console.log(res);
            //   如果请求成功，就跳转至登录页面
            if (res.data.message == "注册成功") {
              //   vant成功提示
              this.$toast.success(res.data.message);
              // 路由跳转
              this.$router.push({ name: "login" });
            } else {
              //   vant失败提示
              this.$toast.fail(res.data.message);
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        this.$toast.fail("注册失败");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
}

.close {
  span {
    font-size: 27 / 360 * 100vw;
  }
}

.logo {
  display: flex;
  justify-content: center;

  span {
    display: block;
    font-size: 126 / 360 * 100vw;
    color: #d81e06;
  }
}

.inputs {
  input {
    margin-bottom: 20px;
  }
}

.tips {
  text-align: right;
  margin-bottom: 20px;

  a {
    color: #3385ff;
  }
}
</style>